﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Common;
using Interface;
using Entities;
using System.ServiceModel;
using System.Runtime.InteropServices;
using System.Collections;
using System.ServiceModel.Channels;
using System.Xml;
using System.ServiceModel.Description;

namespace GUI.DangNhap
{
    public partial class frmXuLy : Form
    {
        public frmXuLy()
        {
            InitializeComponent();
            this.TaiKhoanDangNhap = new TaiKhoan();
        }
        //public delegate void GetBoolean(Entities.TaiKhoan taikhoan);
        //public GetBoolean GetData;

        //[DllImport("user32.dll", EntryPoint = "SendMessageA")]
        //internal static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
        //[DllImport("user32.dll")]
        //internal static extern int ReleaseCapture();
        //const int HTCAPTION = 2;
        //const int WM_NCLBUTTONDOWN = 0xA1;
        //private void frmXuLy_MouseMove(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        //    {
        //        ReleaseCapture();
        //        int Result = SendMessage((int)this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        //    }
        //}

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTenDangNhap.Text.Trim() == "")
                {
                    MessageBox.Show("Nhập tên đăng nhập");
                    txtTenDangNhap.Focus();
                    return;
                }
                if (txtMatKhau.Text.Trim() == "")
                {
                    MessageBox.Show("Nhập mật khẩu");
                    txtMatKhau.Focus();
                    return;
                }
                Entities.TaiKhoan tk = new TaiKhoan();
                tk.HanhDong = "DangNhap";
                tk.TenDangNhap = txtTenDangNhap.Text;
                tk.MatKhauDangNhap = txtMatKhau.Text;
                this.TaiKhoanDangNhap = tk;

                if (this.DangNhap() == true)
                {
                    frmTrangChinh frmMain = new frmTrangChinh();
                    this.Hide();
                    frmMain.Show();
                }
                else
                {
                    //MessageBox.Show("Đăng nhập thất bại!");
                    txtTenDangNhap.Text = "";
                    txtMatKhau.Text = "";
                }

            }
            catch (Exception)
            {

            }
        }

        #region #1
        #region New Object
        private Entities.TaiKhoan TaiKhoanDangNhap = null;
        #endregion

        #region Connect Service
        public string GetConfig(string keys)
        {
            string url = string.Empty;
            try
            {
                System.Reflection.Assembly getfile = System.Reflection.Assembly.GetExecutingAssembly();
                ArrayList arr = new ArrayList();
                XmlDocument xdoc = new System.Xml.XmlDocument();
                xdoc.Load(getfile.Location + ".config");
                foreach (XmlNode xNode in xdoc["configuration"]["appSettings"])
                {
                    if (xNode.Name == "add")
                    {

                        if (xNode.Attributes.GetNamedItem("key").Value.ToUpper() == keys.ToUpper())
                        {
                            url = xNode.Attributes.GetNamedItem("value").Value.ToString();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                url = null;
            }
            return url;
        }
        public Boolean Start()
        {
            try
            {
                string url = GetConfig("Http");
                if (url == null)
                {
                    MessageBox.Show("App.config không timg thấy đường dẫn tới máy chủ");
                    return false;
                }
                EndpointAddress endpoint = new EndpointAddress(url);
                BasicHttpBinding bind = new BasicHttpBinding();
                bind.MaxBufferPoolSize = (int)67108864;
                bind.MaxBufferSize = 67108864;
                bind.MaxReceivedMessageSize = (int)67108864;
                bind.TransferMode = TransferMode.Buffered;
                bind.ReceiveTimeout = new TimeSpan(20, 0, 0);
                bind.Security.Mode = BasicHttpSecurityMode.None;
                bind.CreateBindingElements();
                bind.HostNameComparisonMode = HostNameComparisonMode.Exact;
                bind.TransferMode = TransferMode.Streamed;
                ChannelFactory<Interface.IDataObject> objCF = new ChannelFactory<Interface.IDataObject>(bind, endpoint);
                Common.StaticVariable.InterfaceObject = objCF.CreateChannel();
                Entitystatic.date = Common.StaticVariable.InterfaceObject.SelectDate();
                return true;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                MessageBox.Show("Kết nối service thất bại. Kiểm tra service !");
                Application.Exit();
                return false;
            }
        }
        #endregion

        #region Login
        private Boolean DangNhap()
        {
            Boolean kt = false;
            try
            {
                if (this.TaiKhoanDangNhap.TenDangNhap == null)
                {
                    MessageBox.Show("Tài khoản không đúng");
                    return false;
                }
                if (this.TaiKhoanDangNhap.MatKhauDangNhap == null)
                {
                    MessageBox.Show("Tài khoản không đúng");
                    return false;
                }
                Entities.TaiKhoan tk = Common.StaticVariable.InterfaceObject.DangNhap(TaiKhoanDangNhap.TenDangNhap, TaiKhoanDangNhap.MatKhauDangNhap);
                if (tk == null)
                {
                    MessageBox.Show("Tài khoản không đúng");
                    return false;
                }
                Common.StaticVariable.User = tk;
                Common.Utilities.User = tk;
                UtilitiesVariable.User = tk;
                DataTable dt = Common.StaticVariable.InterfaceObject.SelectChiTietQuyen(tk.TenNhomQuyen);
                if (dt != null)
                {
                    var ar = from a in dt.AsEnumerable()
                             select new Entities.ChiTietQuyen
                                 {
                                     TenNhomQuyen = a.Field<string>("TenNhomQuyen"),
                                     TenForm = a.Field<string>("TenForm"),
                                     Ten = a.Field<string>("Ten"),
                                     QuyenSua = a.Field<bool>("QuyenSua"),
                                     QuyenXem = a.Field<bool>("QuyenXem"),
                                     QuyenThem = a.Field<bool>("QuyenThem"),
                                     QuyenXoa = a.Field<bool>("QuyenXoa")
                                 };
                    Common.StaticVariable.ACTQ = ar.ToArray<Entities.ChiTietQuyen>();
                    Common.Utilities.chitiet = ar.ToArray<Entities.ChiTietQuyen>();
                    UtilitiesVariable.chitiet = Common.Utilities.chitiet;
                }
                else
                {
                    Common.StaticVariable.ACTQ = new ChiTietQuyen[0];
                    Common.Utilities.chitiet = new ChiTietQuyen[0];
                    UtilitiesVariable.chitiet = Common.Utilities.chitiet;
                }
                kt = true;
                this.TaiKhoanDangNhap = null;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = false;
            }
            return kt;
        }
        #endregion

        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                Start();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }


        #endregion
    }
}
#endregion